//
//  frmSettingsController.m
//  jRSSReader
//
//  Created by Jodynek on 6/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "frmSettingsController.h"

NSString * const sUserName        = @"stringUserName";
NSString * const sPassword        = @"stringPassword";
NSString * const iShiftDelay      = @"stringShiftDelay";
NSString * const iRefreshDelay    = @"stringRefreshDelay";

NSString * const prefSavedNotification = @"prefSaved";

@implementation frmSettingsController
@synthesize btnAutoLogon;
@synthesize edtUserName;
@synthesize edtPassword;
@synthesize edtShiftDelay;
@synthesize edtRefreshDelay;

- (id)initWithWindow:(NSWindow *)window
{
  self = [super initWithWindow:window];
  if (self) 
  {
    NSString * appPath = [[NSBundle mainBundle] bundlePath];
    LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if ([self loginItemExistsWithLoginItemReference:loginItems ForPath:appPath])     
    {
      [self setBtnAutoLogonTitle:@"Disable Auto Open at Login"];
    } else {
      [self setBtnAutoLogonTitle:@"Enable Auto Open at Login"];       
    }
  }
  return self;
}

-(void) awakeFromNib
{
  if ([[frmSettingsController userName] isNotEqualTo:@""])
  {
    [edtUserName setStringValue:[frmSettingsController userName]];    
  }
  if ([[frmSettingsController password] isNotEqualTo:@""])
  {
    [edtPassword setStringValue:[frmSettingsController password]];
  }  
  if ([[frmSettingsController shiftDelay] isEqualTo:@""])
  {
    [edtShiftDelay setStringValue:@"300"];    
  } else {
    [edtShiftDelay setStringValue:[frmSettingsController shiftDelay]];
  }
  if ([[frmSettingsController refreshDelay] isEqualTo:@""])
  {
    [edtRefreshDelay setStringValue:@"1800"];    
  } else {
    [edtRefreshDelay setStringValue:[frmSettingsController refreshDelay]];
  }
}

- (void)windowDidLoad
{
  [super windowDidLoad];
}

- (IBAction)btnAutologonClicked:(id)sender 
{
  NSString * appPath = [[NSBundle mainBundle] bundlePath];
  LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  
  if ([self loginItemExistsWithLoginItemReference:loginItems ForPath:appPath])     
  {
    [self disableLoginItemWithLoginItemsReference:loginItems ForPath:appPath];    
    [self setBtnAutoLogonTitle:@"Enable Auto Open at Login"];       
  } else {
    [self enableLoginItemWithLoginItemsReference:loginItems ForPath:appPath];    
    [self setBtnAutoLogonTitle:@"Disable Auto Open at Login"];
  }  
}

+ (NSString *)userName
{
 	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults stringForKey:@"sUserName"];   
}
+ (void)setUserName:(NSString *)sUserName
{
	[[NSUserDefaults standardUserDefaults] setValue:sUserName
                                          forKey:@"sUserName"];    
}

+ (NSString *)password
{
 	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults stringForKey:@"sPassword"];   
}
+ (void)setPassword:(NSString *)sPassword
{
	[[NSUserDefaults standardUserDefaults] setValue:sPassword
                                           forKey:@"sPassword"];    
}

+ (NSString *)shiftDelay
{
 	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults stringForKey:@"iShiftDelay"];   
}
+ (void)setShiftDelay:(NSString *)iShiftDelay
{
	[[NSUserDefaults standardUserDefaults] setValue:iShiftDelay
                                           forKey:@"iShiftDelay"];    
}

+ (NSString *)refreshDelay
{
 	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults stringForKey:@"iRefreshDelay"];   
}
+ (void)setRefreshDelay:(NSString *)iRefreshDelay
{
	[[NSUserDefaults standardUserDefaults] setValue:iRefreshDelay
                                           forKey:@"iRefreshDelay"];    
}

- (IBAction)btnSaveClicked:(id)sender 
{
  [frmSettingsController setUserName:[edtUserName stringValue]];
  [frmSettingsController setPassword:[edtPassword stringValue]];
  [frmSettingsController setShiftDelay:[edtShiftDelay stringValue]];
  [frmSettingsController setRefreshDelay:[edtRefreshDelay stringValue]];
  
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	NSLog(@"Sending notification");
	[nc postNotificationName:prefSavedNotification
                    object:self];  
<<<<<<< HEAD
}

-(NSString*) btnAutoLogonTitle
{
  return btnAutoLogTitle;
}

-(void) setBtnAutoLogonTitle: (NSString *) value
{
  [self willChangeValueForKey:@"btnAutoLogTitle"];
  btnAutoLogTitle = value;
  [self didChangeValueForKey:@"btnAutoLogTitle"];
}

- (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(NSString *)appPath 
{
	// We call LSSharedFileListInsertItemURL to insert the item at the bottom of Login Items list.
	CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
	LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, url, NULL, NULL);		
	if (item)
		CFRelease(item);
}

- (void)disableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(NSString *)appPath 
{
	UInt32 seedValue;
	CFURLRef thePath = NULL;
	// We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)
	// and pop it in an array so we can iterate through it to find our item.
	CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(theLoginItemsRefs, &seedValue);
	for (id item in (__bridge NSArray *)loginItemsArray) {		
		LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
		if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
			if ([[(__bridge NSURL *)thePath path] hasPrefix:appPath]) {
				LSSharedFileListItemRemove(theLoginItemsRefs, itemRef); // Deleting the item
			}
			// Docs for LSSharedFileListItemResolve say we're responsible
			// for releasing the CFURLRef that is returned
			if (thePath != NULL) CFRelease(thePath);
		}		
	}
	if (loginItemsArray != NULL) CFRelease(loginItemsArray);
}

- (BOOL)loginItemExistsWithLoginItemReference:(LSSharedFileListRef)theLoginItemsRefs ForPath:(NSString *)appPath 
{
	BOOL found = NO;  
	UInt32 seedValue;
	CFURLRef thePath = NULL;
  
	// We're going to grab the contents of the shared file list (LSSharedFileListItemRef objects)
	// and pop it in an array so we can iterate through it to find our item.
	CFArrayRef loginItemsArray = LSSharedFileListCopySnapshot(theLoginItemsRefs, &seedValue);
	for (id item in (__bridge NSArray *)loginItemsArray) {    
		LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
		if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
			if ([[(__bridge NSURL *)thePath path] hasPrefix:appPath]) {
				found = YES;
				break;
			}
      // Docs for LSSharedFileListItemResolve say we're responsible
      // for releasing the CFURLRef that is returned
      if (thePath != NULL) CFRelease(thePath);
		}
	}
	if (loginItemsArray != NULL) CFRelease(loginItemsArray);
  
	return found;
=======
>>>>>>> 42acf678065da260e0e994d7eaf924250eff120f
}

@end
